const app = getApp(),url = app.globalData.url;
import util from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr_sex:[{id:1,name:'男',},{id:2,name:'女',}],
    sexindex:null,//选中的id
    marryarr:[{id:1,name:'未婚'},{id:2,name:'已婚'},{id:3,name:'离异'},{id:4,name:'丧偶'}],
    marryindex:null,
    sansan:[
      {id:1,name:'不想换工作'},{id:2,name:'正在观望'},{id:3,name:'正在找工作'}
    ],
    x_id:1,//选中的id
    zzmmList:[
      {id:'群众',name:'群众'},
      {id:'党员',name:'党员'},
      {id:'团员',name:'团员'}
    ],
    zzmmIndex: '',
    zzmmIndex:'',
    index:'',//职位 
    sub:true,//按钮判断
    image:'',
    header:'',
    workYear:'',//参加工作时间
    birthday:'',//生日
    liveAddress:'',// 住址盘判断
    xlindex:'',//index
    xueliArr:[],//学历
    education:'',//upload学历
    marryStatus:'',///婚姻状态
    introduction:"",//简介
    date:'',//生日
    time:'',//参加工作时间
    height:'',//身高
    postcode:'',//邮编
    idCode:'',//身份证号
    wechat:'',//微信
    householdRegister:'',//户籍
    homeAddress:'',//住址
    specialty:'',//电子工程
    personalWeb:'',//网址
    politicCountenance:'',//政治面貌
    region: ['北京市', '北京市', '东城区'],
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          console.log(res)
        if(res.tempFilePaths.length > 0){
            // console.log(2)
            that.setData({image:res.tempFilePaths[0]})
            that.uploadFile(res.tempFilePaths[0])
        }
      }
    })
  },
  uploadFile: function(tempUrl){
    var that = this
    console.log(tempUrl)
    wx.uploadFile({
      url:url+'cvBaseInfo/uploadCVPhoto',
      filePath: tempUrl,
      name: 'cv_photo',
      success: function (res) {
        var ret=JSON.parse(res.data)
        console.log(ret)
        if (ret.code == 200) {
            // console.log(ret.path)
            that.setData({header:ret.data})
            wx.showToast({
              title: ret.msg,
              icon:'none',
            })
        }
      }
    });
  },
  onLoad: function (options) {
    var that = this
    var uid = wx.getStorageSync('user_massage')
    var userInfo = wx.getStorageSync('userInfo')
    that.setData({stsid :options.id})
    //学历
    wx.request({
      url: url + 'dict/getEducationState',
      method:'get',
      data: {},
      success: function(res)
      {
        console.log('学历',res)
        that.setData({
          xueliArr:res.data.data,
          // education:res.data.data[0].subTypeName//第一个学历
          // xueindex:res.data.data[0].id,//第一个id 
          // xueli_size:res.data.data[0].subTypeName,//地一个name
        })
      },
      fail:function(res){
        console.log(res)
      }
    })

    if(options.id == 0 || options.id == undefined){
      that.setData({sttsname:'添加'})
      that.setData({
        name:userInfo.nickName,
        mobileNo:uid.mobileNum,
      })
    }else{
      that.setData({sttsname:'保存'})
      // 根据用户UID获取简历基本信息详情
      wx.request({
        url: url + 'cvBaseInfo/getCVBaseInfoByUserUid?user_uid='+uid.userUid,
        method: "GET",
        success:function(res){
          var ret = res.data
          if(ret.code == 200){
            that.setData({
              ...ret.data,
              image:ret.data.header,//touxiang
              // header:ret.data.header,
              name:ret.data.name ||userInfo.nickName,
              // age:ret.data.age,
              // sex:ret.data.sex,
              x_id:ret.data.findJobStatus,//工作状态
              // findJobStatus:ret.data.findJobStatus,//工作状态
              // positionTitle:ret.data.positionTitle,//申请职位 
              // education:ret.data.education,//xueli
              // birthday:ret.data.birthday,//生日 
              // workYear:ret.data.workYear,//参加工作时间
              // liveAddress:ret.data.liveAddress,//现在住址
              // introduction:ret.data.introduction,//简介
              // mobileNo:ret.data.mobileNo,//电话
              // email:ret.data.email,//邮箱
              // experience:ret.data.experience,//experience 
              // marryStatus:ret.data.marryStatus,
              // homeAddress:ret.data.homeAddress,//家庭住址
              // householdRegister:ret.data.householdRegister,//户籍p
              // politicCountenance:ret.data.politicCountenance,//政治面貌
              // personalWeb:ret.data.personalWeb,//个人主页
              // currentYearWage:ret.data.currentYearWage,
              // baseWage:ret.data.baseWage,
              // allowance:ret.data.allowance,
              // bonus:ret.data.bonus,
              // equityIncome:ret.data.equityIncome,
              // height:ret.data.height,
              // postcode:ret.data.postcode,
              // wechat:ret.data.wechat,
            })
            // sexindex:ret.data.sex -1 || 0,//性别

            if(ret.data.sex == null || ret.data.sex == 0){
              that.setData({sexindex:0,})
            }else{
              that.setData({sexindex:ret.data.sex -1 ,})
            }
            let zzmmIndex = that.data.zzmmList.findIndex(item => item.name === ret.data.politicCountenance)
            that.setData({zzmmIndex})
            if(ret.data.marryStatus >= 1){
              that.setData({marryindex:ret.data.marryStatus -1,})
            }else{
              that.setData({marryindex:ret.data.marryStatus,})
            }
          }else{
            wx.showToast({
              title: ret.msg,
              icon:'none'
            })
          }
        }
      })
    }
  },

  // 输入姓名
  name(e){
    var that= this
    that.setData({name:e.detail.value})  
  },
  // 年龄
  // ageinput(e){
  //   var that= this
  //   that.setData({age:e.detail.value})  
  // },
  // 性别男女
  xingbieChange: function (e) {
    var that = this;
    var val = e.detail.value
    if(val == 0){
      var sex = 1
    }else{
      var sex = 2
    }
    that.setData({sexindex:val,sex:sex})
  },
  marryarrChange: function (e) {
    var that = this;
    var val = e.detail.value
    if(val == 0){
      var marryStatus = 1
    }else if(val == 1){
      var marryStatus = 2
    }else{
      var marryStatus = 3
    }
    that.setData({marryindex:val,marryStatus:marryStatus})
  },
  // 工作状态
  jobstatus(e){
    var that = this
    var stat = e.currentTarget.dataset.id
    that.setData({x_id:stat,findJobStatus:stat})
  },
  // 家庭地址
  jtaddress(e){
    this.setData({homeAddress:e.detail.value})  
  },
  // 户籍
  hjaddress(e){
    this.setData({householdRegister:e.detail.value})  
  },
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value,
      birthday: e.detail.value
    })
  },
  dateDiff: function (date1, type) {
    date1 = new Date(date1).getTime()
    var date2 = new Date().getTime()
    type = type || 'hour'
    var diffValue = Math.abs(date2 - date1)
    var second = 1000
    var minute = second * 60
    var hour = minute * 60
    var day = hour * 24
    var month = day * 30
    var year = month * 12
    var timeType = {
      second: second,
      minute: minute,
      hour: hour,
      day: day,
      month: month,
      year: year
    }

    return Math.ceil(diffValue / timeType[type])
  },
  // 工作时间
  // jobtime(e){
  //   var that= this
  //   console.log(e.detail.value)
  //   that.setData({workYear:e.detail.value})  
  // },
  bindtimeChange: function(e) {
    console.log('bindtimeChange', e.detail.value)
    this.setData({
      time: e.detail.value,
      workYear: e.detail.value
    })
  },
  // 工作jingyan
  experience(e){
    var that= this
    console.log(e.detail.value)
    that.setData({experience:e.detail.value})  
  },
  // 政治面貌
  zzmmChange(e){
    let zzmmIndex = e.detail.value
    let zzmmList = this.data.zzmmList
    this.setData({
      zzmmIndex,
      politicCountenance:zzmmList[zzmmIndex].name
    })
  },
  xinziChange: function (e) {
    var that = this;
    var val = e.detail.value//这个是  index
    var skill = that.data.xueliArr
    for(let item in skill){
      if(item == val){
        that.setData({education:skill[item].subTypeName,})
        break;
      }
    }
    that.setData({xlindex:val,})//页面赋值 title:name
  },
  // 职位
  positionTitle(e){
    var that= this
    console.log(e.detail.value)
    that.setData({positionTitle:e.detail.value}) 
  },
  // 居住地
  zhuzhi(e){
    var that= this
    console.log(e.detail.value)
    that.setData({liveAddress:e.detail.value}) 
  },
  // 简介
  des(e){
    var that= this
    console.log(e.detail.value)
    that.setData({introduction:e.detail.value}) 
  },
  currentYearWage(e){
    var that= this
    console.log(e.detail.value)
    that.setData({currentYearWage:e.detail.value}) 
  },
  // 输入基本工资
  baseWage(e){
    var that= this
    console.log(e.detail.value)
    that.setData({baseWage:e.detail.value}) 
  },
  // 输入补贴/津贴
  allowance(e){
    var that= this
    that.setData({allowance:e.detail.value}) 
  },
  // 输入股权收益
  equityIncome(e){
    var that= this
    that.setData({equityIncome:e.detail.value}) 
  },
  // 输入奖金/佣金
  bonus(e){
    this.setData({bonus:e.detail.value}) 
  },

  idCodeInput(e){
    this.setData({
      idCode : e.detail.value.replace(/\s+/g,""),
    });
  },
  phone(e){
    this.setData({mobileNo:e.detail.value}) 
  },
  email(e){
    var that= this
    console.log(e.detail.value)
    that.setData({email:e.detail.value}) 
  },
  checkSensWords(e){
    let itemData = e.target.dataset;
    let label = Object.values(itemData)[0]
    let key = Object.keys(itemData)[0]
    this.sensWords = this.sensWords || {}
    app.validSensWords(e.detail.value, label).then(() => {
      delete this.sensWords[key]
    }).catch(() => {
      this.sensWords = {...itemData, ...this.sensWords}
    })
  },
  submit(){
    var that = this
    var uid = wx.getStorageSync('user_massage')
    var stsid = that.data.stsid
    if(stsid == 0 || stsid == undefined){//添加
      var urlapi = "cvBaseInfo/wxAddCVInfo"//cvBaseInfo/add
      var mothed = "POST"

    }else{//编辑
      var urlapi = "cvBaseInfo/update"
      var mothed = "PUT"
    }
    
    
    if(!/^1\d{10}$/.test(that.data.mobileNo)){
			wx.showToast({
        title: '手机号有误！',
        icon:'none',
      })
			return false;
		}
    var youxiang = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
    if(!youxiang.test(that.data.email)){
			wx.showToast({
        title: '邮箱有误！',
        icon:'none',
      })
			return false;
		}
    if(!this.data.idCode){
      wx.showToast({
        title: '请输入身份证号',
        icon:'none',
      })
			return false;
    }
    if(!util.isIdCode(this.data.idCode)){
      wx.showToast({
        title: '身份证号有误',
        icon:'none',
      })
			return false;
    }
    if(!that.data.birthday){
      wx.showToast({
        title: '请输入生日',
        icon:'none',
      })
      return false
    }
    if(!this.data.politicCountenance){
      wx.showToast({
        title: '请选择政治面貌',
        icon:'none',
      })
      return false
    }
    if(!this.data.householdRegister){
      wx.showToast({
        title: '请输入户籍所在地',
        icon:'none',
      })
      return false
    }
    if(!this.data.homeAddress){
      wx.showToast({
        title: '请输入家庭住址',
        icon:'none',
      })
      return false
    }
    if(!that.data.workYear){
      wx.showToast({
        title: '请输入工作时间',
        icon:'none',
      })
      return false
    }

    let sensWords = Object.values(this.sensWords  || {})
    if(sensWords.length > 0){
      wx.showToast({
        title: `${sensWords.join(',')}包含敏感词，请修改后再${that.data.sttsname}`,
        icon:'none',
      })
			return false;
    }
    var data = {
      'id':stsid,
      approval: '1',
      "userUid": uid.userUid,
      "header": that.data.header,
      "name":that.data.name,
      "mobileNo":that.data.mobileNo,
      "sex": that.data.sex,
      "birthday": that.data.birthday,
      "workYear": that.data.workYear,//工作时间
      "findJobStatus":that.data.findJobStatus,//当前状态
      "liveAddress": that.data.liveAddress,//住址
      "introduction": that.data.introduction,//简介
      "email": that.data.email,//邮箱
      "age": that.dateDiff(that.data.birthday, 'year'),
      "education": that.data.education,
      "positionTitle": that.data.positionTitle,
      "experience": that.dateDiff(that.data.workYear, 'year'),
      "marryStatus": that.data.marryStatus,
      "householdRegister": that.data.householdRegister,//沈阳
      "specialty": that.data.specialty,//电子工程
      "idCode": that.data.idCode,//210104198503093345
      "wechat": that.data.wechat,//wx3345
      "homeAddress":that.data.homeAddress,//沈阳市和平区
      "personalWeb": that.data.personalWeb,//www.baidu.com
      "currentYearWage":that.data.currentYearWage,
      "baseWage":that.data.baseWage,
      "allowance":that.data.allowance,
      "bonus":that.data.bonus,
      "equityIncome":that.data.equityIncome,
      "politicCountenance": that.data.politicCountenance,//党员
      "height": that.data.height,// 183
      "postcode": that.data.postcode,// 110024
    };
    that.setData({sub:false})
    wx.request({
      url:url+urlapi,
      header:{
        'context-type': 'application/json',
      },
      method: mothed,
      data:JSON.stringify(data),
      success:function(res){
        var ret = res.data
        that.setData({sub:true})
        if(ret.code == 200){
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
          
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var userInfo = wx.getStorageSync('userInfo')
    // console.log(userInfo)
    that.setData({
      userInfo
    })
  },

})